package com.ruoyi.wb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbClassifyYear;
import com.ruoyi.wb.domain.WbClassifyYearContent;
import com.ruoyi.wb.domain.WbClassifyYearContentThree;
import com.ruoyi.wb.dto.WbClassifyDelDto;
import com.ruoyi.wb.dto.WbClassifyDto;
import com.ruoyi.wb.dto.WbClassifyYearDto;
import com.ruoyi.wb.enmus.PlanTypeEnums;
import com.ruoyi.wb.mapper.WbClassifyYearContentMapper;
import com.ruoyi.wb.mapper.WbClassifyYearContentThreeMapper;
import com.ruoyi.wb.mapper.WbClassifyYearMapper;
import com.ruoyi.wb.service.IWbClassifyYearService;
import com.ruoyi.wb.vo.WbClassifyYearContentThreeVo;
import com.ruoyi.wb.vo.WbClassifyYearContentVo;
import com.ruoyi.wb.vo.WbClassifyYearVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 维保年度类目列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class WbClassifyYearServiceImpl implements IWbClassifyYearService 
{
    @Autowired
    private WbClassifyYearMapper wbClassifyYearMapper;

    @Autowired
    private WbClassifyYearContentMapper wbClassifyYearContentMapper;

    @Autowired
    private WbClassifyYearContentServiceImpl wbClassifyYearContentService;


    @Autowired
    private WbClassifyYearContentThreeMapper wbClassifyYearContentThreeMapper;
 @Autowired
    private WbClassifyYearContentThreeServiceImpl wbClassifyYearContentThreeService;

    /**
     * 查询维保年度类目列表
     * 
     * @param id 维保年度类目列表主键
     * @return 维保年度类目列表
     */
    @Override
    public WbClassifyYear selectWbClassifyYearById(Long id)
    {
        return wbClassifyYearMapper.selectWbClassifyYearById(id);
    }

    /**
     * 查询维保年度类目列表列表
     * 
     * @param wbClassifyYear 维保年度类目列表
     * @return 维保年度类目列表
     */
    @Override
    public List<WbClassifyYear> selectWbClassifyYearList(WbClassifyYear wbClassifyYear)
    {
        return wbClassifyYearMapper.selectWbClassifyYearList(wbClassifyYear);
    }

    /**
     * 新增维保年度类目列表
     * 
     * @param wbClassifyYear 维保年度类目列表
     * @return 结果
     */
    @Override
    public int insertWbClassifyYear(WbClassifyYear wbClassifyYear)
    {
        wbClassifyYear.setCreateTime(DateUtils.getNowDate());
        return wbClassifyYearMapper.insertWbClassifyYear(wbClassifyYear);
    }

    /**
     * 修改维保年度类目列表
     * 
     * @param wbClassifyYear 维保年度类目列表
     * @return 结果
     */
    @Override
    public int updateWbClassifyYear(WbClassifyYear wbClassifyYear)
    {
        wbClassifyYear.setUpdateTime(DateUtils.getNowDate());
        return wbClassifyYearMapper.updateWbClassifyYear(wbClassifyYear);
    }

    /**
     * 批量删除维保年度类目列表
     * 
     * @param ids 需要删除的维保年度类目列表主键
     * @return 结果
     */
    @Override
    public int deleteWbClassifyYearByIds(Long[] ids)
    {
        return wbClassifyYearMapper.deleteWbClassifyYearByIds(ids);
    }

    /**
     * 删除维保年度类目列表信息
     * 
     * @param id 维保年度类目列表主键
     * @return 结果
     */
    @Override
    public int deleteWbClassifyYearById(Long id)
    {
        return wbClassifyYearMapper.deleteWbClassifyYearById(id);
    }

    /**
     * 新增年度类目模板 包含子集
     * @param wbClassifyYearDto
     * @return
     */
    @Transactional
    @Override
    public int addAll(WbClassifyYearDto wbClassifyYearDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        wbClassifyYearDto.setCreateTime(DateUtils.getNowDate());
        wbClassifyYearDto.setCreateBy(loginUser.getUserId().toString());
        wbClassifyYearDto.setUpdateTime(DateUtils.getNowDate());
        wbClassifyYearDto.setUpdateBy(loginUser.getUserId().toString());
        //新增一级模板
        WbClassifyYear wbClassifyYear = BeanUtil.copyProperties(wbClassifyYearDto, WbClassifyYear.class);

        int i = wbClassifyYearMapper.insertWbClassifyYear(wbClassifyYear);
       if (i==0){
           throw new RuntimeException("新增年度计划一级目录失败");
       }
       //校验
       if (CollectionUtils.isEmpty(wbClassifyYearDto.getWbClassifyYearContentVos())){
         return i;
       }
       //获取二级目录
        List<WbClassifyYearContentVo> wbClassifyYearContentVos = wbClassifyYearDto.getWbClassifyYearContentVos();
        wbClassifyYearContentVos.forEach(item->{
            item.setCreateTime(DateUtils.getNowDate());
            item.setCreateBy(loginUser.getUserId().toString());
            item.setUpdateTime(DateUtils.getNowDate());
            item.setUpdateBy(loginUser.getUserId().toString());
        });
        //赋值
        wbClassifyYearContentVos.forEach(item->item.setClassifyYearId(wbClassifyYear.getId()));
        //批量新增二级目录
        int j = wbClassifyYearContentMapper.batchInsert(wbClassifyYearContentVos);
        if (j==0){
            throw new RuntimeException("新增年度计划二级目录失败");
        }

        //三级目录
        List<WbClassifyYearContentThreeVo> wbClassifyYearContentThreeVos = new ArrayList<>();

        //赋值
        wbClassifyYearContentVos.forEach(obj->obj.getWbClassifyYearContentThreeVos().forEach(obj2->{
            obj2.setClassifyYearContentId(obj.getClassifyYearContentId());
            obj2.setCreateTime(DateUtils.getNowDate());
            obj2.setCreateBy(loginUser.getUserId().toString());
            obj2.setUpdateTime(DateUtils.getNowDate());
            obj2.setUpdateBy(loginUser.getUserId().toString());
        }));


        //获取三级目录
        wbClassifyYearContentVos.stream().map(WbClassifyYearContentVo::getWbClassifyYearContentThreeVos).collect(Collectors.toList()).forEach(wbClassifyYearContentThreeVos::addAll);

        //新增三级模板
        if (CollectionUtils.isEmpty(wbClassifyYearContentThreeVos)){
            return i;
        }
        //转换
        List<WbClassifyYearContentThree> wbClassifyYearContentThrees = BeanUtil.copyToList(wbClassifyYearContentThreeVos, WbClassifyYearContentThree.class);
        int k = wbClassifyYearContentThreeMapper.batchInsert(wbClassifyYearContentThrees);
        if (k==0){
            throw new RuntimeException("新增年度计划三级目录失败");
        }
        return i;
    }

    /**
     * 批量查询年度计划包括下面所有级
     * @param wbClassifyYear
     * @return
     */
    @Override
    public List<WbClassifyYearVo> allList(WbClassifyYear wbClassifyYear) {
        List<WbClassifyYearVo> wbClassifyYearVos = new ArrayList<>();
        //一级目录
        List<WbClassifyYear> wbClassifyYears = Collections.unmodifiableList(wbClassifyYearMapper.selectWbClassifyYearList(wbClassifyYear));

        //校验
        if (CollectionUtils.isEmpty(wbClassifyYears)){
            return wbClassifyYearVos;
        }
        //转换一级返回
        wbClassifyYearVos = BeanUtil.copyToList(wbClassifyYears, WbClassifyYearVo.class);
        //校验
        List<Long> WbClassifyYearIds = wbClassifyYears.stream().map(WbClassifyYear::getId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(WbClassifyYearIds)){
            return wbClassifyYearVos;
        }
        wbClassifyYearVos.sort((a,b)->a.getSort().compareTo(b.getSort()));
        //查询二级目录
        List<WbClassifyYearContent> wbClassifyYearContents = wbClassifyYearContentMapper.selectWbClassifyYearContentListByWbClassifyYearId(WbClassifyYearIds);
        if (CollectionUtils.isEmpty(wbClassifyYearContents)){
            return wbClassifyYearVos;
        }
        //转换为二级返回
        List<WbClassifyYearContentVo> wbClassifyYearContentVos = BeanUtil.copyToList(wbClassifyYearContents, WbClassifyYearContentVo.class);


        List<Long> classifyYearContentIds = wbClassifyYearContentVos.stream().map(WbClassifyYearContentVo::getClassifyYearContentId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(classifyYearContentIds)){
            return wbClassifyYearVos;
        }
        wbClassifyYearContentVos.sort((a,b)->a.getSort().compareTo(b.getSort()));
        //查询三级目录
        List<WbClassifyYearContentThree> wbClassifyYearContentThrees =wbClassifyYearContentThreeMapper.selectWbClassifyYearContentThreeListByClassifyYearContentIds(classifyYearContentIds);

        if (CollectionUtils.isEmpty(wbClassifyYearContentThrees)){
            //二级装入一级
            Map<Long, List<WbClassifyYearContentVo>> mapTwo = new HashMap<>();
            wbClassifyYearContentVos.forEach(item->{
                if (mapTwo.containsKey(item.getClassifyYearId())){
                    mapTwo.get(item.getClassifyYearId()).add(item);
                }else {
                    List<WbClassifyYearContentVo> itemWbClassifyYearContentVos = new ArrayList<>();
                    itemWbClassifyYearContentVos.add(item);
                    mapTwo.put(item.getClassifyYearId(),itemWbClassifyYearContentVos);
                }
            });
            wbClassifyYearVos.forEach(item->{
                if (mapTwo.containsKey(item.getId())){
                    item.setWbClassifyYearContentVos(mapTwo.get(item.getId()));
                }
            });
            return wbClassifyYearVos;
        }
        //转换为三级返回
        List<WbClassifyYearContentThreeVo> wbClassifyYearContentThreeVos = BeanUtil.copyToList(wbClassifyYearContentThrees, WbClassifyYearContentThreeVo.class);
        wbClassifyYearContentThreeVos.sort((a,b)->a.getSort().compareTo(b.getSort()));
        //三级装入二级
        Map<Long, List<WbClassifyYearContentThreeVo>> mapThree = new HashMap<>();
        wbClassifyYearContentThreeVos.forEach(item->{
            if (mapThree.containsKey(item.getClassifyYearContentId())){
                mapThree.get(item.getClassifyYearContentId()).add(item);
            }else {
                List<WbClassifyYearContentThreeVo> itemWbClassifyYearContentThreeVos = new ArrayList<>();
                itemWbClassifyYearContentThreeVos.add(item);
                mapThree.put(item.getClassifyYearContentId(),itemWbClassifyYearContentThreeVos);
            }
        });
        wbClassifyYearContentVos.forEach(item->{
            if (mapThree.containsKey(item.getClassifyYearContentId())){
                item.setWbClassifyYearContentThreeVos(mapThree.get(item.getClassifyYearContentId()));
            }
        });
        //二级装入一级
        Map<Long, List<WbClassifyYearContentVo>> mapTwo = new HashMap<>();
        wbClassifyYearContentVos.forEach(item->{
            if (mapTwo.containsKey(item.getClassifyYearId())){
                mapTwo.get(item.getClassifyYearId()).add(item);
            }else {
                List<WbClassifyYearContentVo> itemWbClassifyYearContentVos = new ArrayList<>();
                itemWbClassifyYearContentVos.add(item);
                mapTwo.put(item.getClassifyYearId(),itemWbClassifyYearContentVos);
            }
        });
        wbClassifyYearVos.forEach(item->{
            if (mapTwo.containsKey(item.getId())){
                item.setWbClassifyYearContentVos(mapTwo.get(item.getId()));
            }
        });
        return wbClassifyYearVos;
    }

    /**
     * 新增模板 一级 或二级 或三级
     */
    @Override
    public int addOne(WbClassifyDto wbClassifyDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int i=0;
        if (PlanTypeEnums.ONE_RANK.getType().equals(wbClassifyDto.getTier())) {
            WbClassifyYear wbClassifyYear = new WbClassifyYear();
            wbClassifyYear.setTier(Long.parseLong(PlanTypeEnums.ONE_RANK.getType()));
            wbClassifyYear.setClassifyName(wbClassifyDto.getClassifyName());
            wbClassifyYear.setSort(wbClassifyDto.getSort());
            wbClassifyYear.setRemark(wbClassifyDto.getRemark());
            wbClassifyYear.setStatus(wbClassifyDto.getStatus());
            wbClassifyYear.setCreateTime(DateUtils.getNowDate());
            wbClassifyYear.setCreateBy(loginUser.getUserId().toString());
            wbClassifyYear.setUpdateTime(DateUtils.getNowDate());
            wbClassifyYear.setUpdateBy(loginUser.getUserId().toString());
            i=wbClassifyYearMapper.insertWbClassifyYear(wbClassifyYear);
        }else if(PlanTypeEnums.TWO_RANK.getType().equals(wbClassifyDto.getTier())){
            WbClassifyYearContent wbClassifyYearContent = new WbClassifyYearContent();
            wbClassifyYearContent.setTier(Long.parseLong(PlanTypeEnums.TWO_RANK.getType()));
            wbClassifyYearContent.setClassifyYearId(wbClassifyDto.getParentId());
            wbClassifyYearContent.setClassifyName(wbClassifyDto.getClassifyName());
            wbClassifyYearContent.setRemark(wbClassifyDto.getRemark());
            wbClassifyYearContent.setStatus(wbClassifyDto.getStatus());
            wbClassifyYearContent.setSort(wbClassifyDto.getSort());
            wbClassifyYearContent.setCreateTime(DateUtils.getNowDate());
            wbClassifyYearContent.setCreateBy(loginUser.getUserId().toString());
            wbClassifyYearContent.setUpdateTime(DateUtils.getNowDate());
            wbClassifyYearContent.setUpdateBy(loginUser.getUserId().toString());
            i=wbClassifyYearContentMapper.insertWbClassifyYearContent(wbClassifyYearContent);
        }else if(PlanTypeEnums.THREE_RANK.getType().equals(wbClassifyDto.getTier())){
            WbClassifyYearContentThree wbClassifyYearContentThree = new WbClassifyYearContentThree();
            wbClassifyYearContentThree.setTier(Long.parseLong(PlanTypeEnums.THREE_RANK.getType()));
            wbClassifyYearContentThree.setClassifyYearContentId(wbClassifyDto.getParentId());
            wbClassifyYearContentThree.setClassifyName(wbClassifyDto.getClassifyName());
            wbClassifyYearContentThree.setRemark(wbClassifyDto.getRemark());
            wbClassifyYearContentThree.setStatus(wbClassifyDto.getStatus());
            wbClassifyYearContentThree.setSort(wbClassifyDto.getSort());
            wbClassifyYearContentThree.setStandardRequest(wbClassifyDto.getStandardRequest());
            wbClassifyYearContentThree.setCreateTime(DateUtils.getNowDate());
            wbClassifyYearContentThree.setCreateBy(loginUser.getUserId().toString());
            wbClassifyYearContentThree.setUpdateTime(DateUtils.getNowDate());
            wbClassifyYearContentThree.setUpdateBy(loginUser.getUserId().toString());
            i=wbClassifyYearContentThreeMapper.insertWbClassifyYearContentThree(wbClassifyYearContentThree);
        }
        return i;
    }

    /**
     * 修改模板 一级 或二级 或三级
     */
    @Override
    public int editOne(WbClassifyDto wbClassifyDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        int i=0;
        if (PlanTypeEnums.ONE_RANK.getType().equals(wbClassifyDto.getTier())) {
            WbClassifyYear wbClassifyYear = new WbClassifyYear();
            wbClassifyYear.setClassifyName(wbClassifyDto.getClassifyName());
            wbClassifyYear.setId(wbClassifyDto.getId());
            wbClassifyYear.setSort(wbClassifyDto.getSort());
            wbClassifyYear.setRemark(wbClassifyDto.getRemark());
            wbClassifyYear.setStatus(wbClassifyDto.getStatus());
            wbClassifyYear.setUpdateTime(DateUtils.getNowDate());
            wbClassifyYear.setUpdateBy(loginUser.getUserId().toString());
            i=wbClassifyYearMapper.updateWbClassifyYear(wbClassifyYear);
        }else if(PlanTypeEnums.TWO_RANK.getType().equals(wbClassifyDto.getTier())){
            WbClassifyYearContent wbClassifyYearContent = new WbClassifyYearContent();
            wbClassifyYearContent.setClassifyYearContentId(wbClassifyDto.getId());
            wbClassifyYearContent.setClassifyYearId(wbClassifyDto.getParentId());
            wbClassifyYearContent.setClassifyName(wbClassifyDto.getClassifyName());
            wbClassifyYearContent.setRemark(wbClassifyDto.getRemark());
            wbClassifyYearContent.setStatus(wbClassifyDto.getStatus());
            wbClassifyYearContent.setSort(wbClassifyDto.getSort());
            wbClassifyYearContent.setUpdateTime(DateUtils.getNowDate());
            wbClassifyYearContent.setUpdateBy(loginUser.getUserId().toString());
            i=wbClassifyYearContentMapper.updateWbClassifyYearContent(wbClassifyYearContent);
        }else if(PlanTypeEnums.THREE_RANK.getType().equals(wbClassifyDto.getTier())){
            WbClassifyYearContentThree wbClassifyYearContentThree = new WbClassifyYearContentThree();
            wbClassifyYearContentThree.setClassifyContentId(wbClassifyDto.getId());
            wbClassifyYearContentThree.setClassifyYearContentId(wbClassifyDto.getParentId());
            wbClassifyYearContentThree.setClassifyName(wbClassifyDto.getClassifyName());
            wbClassifyYearContentThree.setRemark(wbClassifyDto.getRemark());
            wbClassifyYearContentThree.setStatus(wbClassifyDto.getStatus());
            wbClassifyYearContentThree.setSort(wbClassifyDto.getSort());
            wbClassifyYearContentThree.setUpdateTime(DateUtils.getNowDate());
            wbClassifyYearContentThree.setUpdateBy(loginUser.getUserId().toString());
            wbClassifyYearContentThree.setStandardRequest(wbClassifyDto.getStandardRequest());
            i=wbClassifyYearContentThreeMapper.updateWbClassifyYearContentThree(wbClassifyYearContentThree);
        }
        return i;
    }

    /**
     * 批量删除模板 一级 或二级 或三级
     */
    @Override
    @Transactional
    public int removeAll(WbClassifyDelDto wbClassifyDelDto) {

        List<Long> oneRankIds = wbClassifyDelDto.getOneRankIds();
        if (!CollectionUtils.isEmpty(oneRankIds)){
            int j = wbClassifyYearMapper.deleteWbClassifyYearByIds(oneRankIds.toArray(Long[]::new));
            int i = wbClassifyYearContentService.deleteWbClassifyYearContentByParentIds(oneRankIds);
            if (j!=oneRankIds.size()||i==0){
                throw new RuntimeException("删除一级目录失败");
            }
        }
        List<Long> twoRankIds = wbClassifyDelDto.getTwoRankIds();
        if (!CollectionUtils.isEmpty(twoRankIds)){
            int j = wbClassifyYearContentMapper.deleteWbClassifyYearContentByClassifyYearContentIds(twoRankIds.toArray(Long[]::new));
            List<WbClassifyYearContentThree> wbClassifyYearContentThrees = wbClassifyYearContentThreeService.selectWbClassifyYearContentThreeListByClassifyYearContentIds(twoRankIds);
            int i = wbClassifyYearContentThreeService.deleteWbClassifyYearContentThreeByParentIds(twoRankIds);

            if (j!=twoRankIds.size()||i!=wbClassifyYearContentThrees.size()){
                throw new RuntimeException("删除二级目录失败");
            }
        }
        List<Long> threeRankIds = wbClassifyDelDto.getThreeRankIds();
        if (!CollectionUtils.isEmpty(threeRankIds)){
            int j =  wbClassifyYearContentThreeMapper.deleteWbClassifyYearContentThreeByClassifyContentIds(threeRankIds.toArray(Long[]::new));
            if (j!=threeRankIds.size()){
                throw new RuntimeException("删除三级目录失败");
            }
        }
        return 1;
    }
}
